import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class ScaffTest2 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ScaffTest2State();
  }
}

class _ScaffTest2State extends State<ScaffTest2>
    with SingleTickerProviderStateMixin {
  var tabs = ['GradView', "Gradview2", '测试3', '测试4'];
  List<IconData> _list = [];
  TabController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = TabController(length: tabs.length, vsync: this);
    _retrieveIcons();
  }

  void _retrieveIcons() {
    Future.delayed(Duration(seconds: 2)).then((e) {
      setState(() {
        _list.addAll([
          Icons.ac_unit,
          Icons.airport_shuttle,
          Icons.all_inclusive,
          Icons.beach_access,
          Icons.cake,
          Icons.free_breakfast
        ]);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'ListView',
          style: TextStyle(color: Colors.black),
        ),
        leading: Builder(
          builder: (context) {
            return IconButton(
              icon: Icon(
                Icons.calendar_view_day,
                color: Colors.black,
              ),
              onPressed: () {
                Scaffold.of(context).openDrawer();
              },
            );
          },
        ),
        backgroundColor: Colors.white,
        bottom: TabBar(
          isScrollable: true,
          tabs: tabs
              .map((y) => Tab(
                    text: y,
                  ))
              .toList(),
          unselectedLabelColor: Colors.black12,
          labelColor: Colors.black,
          controller: _controller,
        ),
      ),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            UserAccountsDrawerHeader(
              accountName: Text('张晨'),
              accountEmail: Text('1170184770@qq.com'),
              otherAccountsPictures: <Widget>[
                Icon(
                  Icons.add_a_photo,
                  color: Colors.white,
                ),
                Icon(
                  Icons.image,
                  color: Colors.white,
                ),
              ],
              currentAccountPicture: ClipOval(
                child: Image.asset(
                  'geo1.jpg',
                  width: 100,
                ),
              ),
            ),
            ListTile(
              leading: Icon(
                Icons.home,
                size: 30,
              ),
              title: Text(
                '首页',
                style: TextStyle(fontSize: 15),
              ),
              trailing: Icon(Icons.keyboard_arrow_right),
              dense: true,
              onTap: () {},
            ),
            Divider(
              height: 1,
            ),
            ListTile(
              leading: Icon(
                Icons.access_time,
                size: 30,
              ),
              title: Text(
                '历史纪录',
                style: TextStyle(fontSize: 15),
              ),
              dense: true,
              trailing: Icon(Icons.keyboard_arrow_right),
              onTap: () {},
            ),
            Divider(
              height: 1,
            ),
            ListTile(
              leading: Icon(
                Icons.archive,
                size: 30,
              ),
              title: Text(
                '下载纪录',
                style: TextStyle(fontSize: 15),
              ),
              dense: true,
              trailing: Icon(Icons.keyboard_arrow_right),
              onTap: () {},
            ),
            Divider(
              height: 1,
            ),
            ListTile(
              leading: Icon(
                Icons.bookmark_border,
                size: 30,
              ),
              title: Text(
                '我的收藏',
                style: TextStyle(fontSize: 15),
              ),
              dense: true,
              trailing: Icon(Icons.keyboard_arrow_right),
              onTap: () {},
            )
          ],
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.white,
        tooltip: "_add",
        child: Icon(
          Icons.accessibility,
          color: Colors.black,
        ),
        onPressed: () {
          Fluttertoast.showToast(msg: '点击了');
        },
      ),
      bottomNavigationBar: BottomAppBar(
          shape: CircularNotchedRectangle(),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.account_balance),
                onPressed: () {},
              ),
              SizedBox(),
              IconButton(
                icon: Icon(Icons.accessibility),
                onPressed: () {},
              )
            ],
          )),
      body: TabBarView(
        controller: _controller,
        children: tabs.map((f) {
          return _widget(f);
        }).toList(),
      ),
    );
  }

  Widget _widget(var e) {
    if (e == 'GradView') {
      return GridView(
        physics: BouncingScrollPhysics(),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            childAspectRatio: 1.0,
            crossAxisSpacing: 1.0,
            mainAxisSpacing: 1.0),
        children: <Widget>[
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.ac_unit),
          ),
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.airport_shuttle),
          ),
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.all_inclusive),
          ),
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.beach_access),
          ),
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.cake),
          ),
          DecoratedBox(
              decoration: BoxDecoration(
                  color: Colors.green, borderRadius: BorderRadius.zero),
              child: Icon(Icons.free_breakfast)),
        ],
      );
    } else if (e == 'Gradview2') {
      return GridView.extent(
        maxCrossAxisExtent: 150,
        childAspectRatio: 1.0,
        mainAxisSpacing: 1.0,
        crossAxisSpacing: 1.0,
        children: <Widget>[
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.ac_unit),
          ),
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.airport_shuttle),
          ),
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.all_inclusive),
          ),
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.beach_access),
          ),
          DecoratedBox(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.zero),
            child: Icon(Icons.cake),
          ),
          DecoratedBox(
              decoration: BoxDecoration(
                  color: Colors.green, borderRadius: BorderRadius.zero),
              child: Icon(Icons.free_breakfast)),
        ],
      );
    } else if (e == '测试3') {
      return GridView.custom(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            mainAxisSpacing: 10,
            crossAxisSpacing: 10,
            childAspectRatio: 1,
          ),
          childrenDelegate: SliverChildBuilderDelegate(
            (context, position) {
              return Container(
                alignment: Alignment.center,
                color: Color(0xfff1f1f1),
                child: Text("aaaaaaaaaaaaaaaa"),
              );
            },
            childCount: 20,
          ));
    } else if (e == '测试4') {
      return GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4, childAspectRatio: 1.0),
        itemBuilder: (context, index) {
          if (_list.length < 200) {
            _retrieveIcons();
          }
          return Icon(_list[index]);
        },
        itemCount: _list.length,
      );
    } else {
      return Center(
        child: Text(
          e,
          style: TextStyle(fontSize: 45, color: Colors.cyan),
        ),
      );
    }
    return null;
  }
}
